package com.itheima.service;

import com.itheima.bean.Student;
import com.itheima.mapper.Student_TestIf;
import com.itheima.utils.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
public class StudentService implements Student_TestIf {
    @Override
    public List<Student> selectAll() throws Exception {
        SqlSession sqlSession = Utils.getSqlSession();
        Student_TestIf mapper = sqlSession.getMapper(Student_TestIf.class);

        List<Student> students = mapper.selectAll();
        sqlSession.close();

        return students;
    }
    @Override
    public Integer insert(Student student) throws Exception {
        SqlSession sqlSession = Utils.getSqlSession();

        Student_TestIf mapper = sqlSession.getMapper(Student_TestIf.class);
        Integer insert = mapper.insert(student);

        sqlSession.commit();
        sqlSession.close();
        return insert;
    }
    @Override
    public Student selectById(Integer id) throws Exception {
        SqlSession sqlSession = Utils.getSqlSession();

        Student_TestIf mapper = sqlSession.getMapper(Student_TestIf.class);
        Student student = mapper.selectById(id);

        sqlSession.close();
        return student;
    }
    @Override
    public Integer delete(Integer id) throws Exception {
        SqlSession sqlSession = Utils.getSqlSession();

        Student_TestIf mapper = sqlSession.getMapper(Student_TestIf.class);
        Integer delete = mapper.delete(id);

        sqlSession.commit();
        sqlSession.close();

        return delete;
    }

    @Override
    public Integer update(Student student) throws Exception{
        SqlSession sqlSession = Utils.getSqlSession();
        Student_TestIf mapper = sqlSession.getMapper(Student_TestIf.class);
        Integer i = mapper.update(student);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
